package com.home766.controller.util;

import com.home766.util.DateTime;
import com.home766.util.PaymentUtil;
import com.home766.util.StringUtil;
import net.biz.controller.admin.BaseController;
import net.biz.entity.Order;
import net.biz.service.OrderService;
import net.biz.util.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/tlpay")
public class TLPAYPaymentController extends BaseController {

	@Resource(name = "orderServiceImpl")
	private OrderService orderServiceImpl;
	
	@RequestMapping(value="/payOrder",produces=("text/plain; charset=utf-8"))
	public String payOrder(HttpServletRequest request, ModelMap model){
		System.out.println("！!_!_!_!_!!-----!!_____!_!!!_!!进入支付前台回调方法——!_!_!_!_!____!_!____！！——！——！——！__!_!_!!!");
		String token=request.getParameter("token");
		String sn=request.getParameter("orderNo");//订单编号
		String amount=request.getParameter("orderAmount");//订单金额
		String paymentOrderId=request.getParameter("paymentOrderId");//支付编号
		String orderDatetime=request.getParameter("orderDatetime");//订单提交时间
		String payDatetime=request.getParameter("payDatetime");//订单支付时间
		String requestType=request.getParameter("requestType");//请求类型
		if(StringUtil.isNullOrSpace(requestType)){
			requestType="2";
		}
		BigDecimal price=new BigDecimal(amount);
		price=price.divide(new BigDecimal("100"),2, BigDecimal.ROUND_HALF_UP);
		orderServiceImpl.updatePaymentStatus(sn, price,paymentOrderId,orderDatetime,payDatetime,getCurrent(),null,null);
		model.addAttribute("sn",sn);
		model.addAttribute("amount",price);
		model.addAttribute("paymentOrderId",paymentOrderId);
		model.addAttribute("orderDatetime",DateTime.dateTimeToString(DateTime.StringToDateForPay(orderDatetime)));
		model.addAttribute("payDatetime",DateTime.dateTimeToString(DateTime.StringToDateForPay(payDatetime)));
		model.addAttribute("requestType", Integer.parseInt(String.valueOf(requestType)));
//		return "订单编号： "+sn+"支付编号： "+paymentOrderId+"订单金额： "+amount+"提交时间： "+orderDatetime+"支付时间： "+payDatetime;
		return "/pay/success";
	}

	@ResponseBody
	@RequestMapping(value="/checkPayUpdate",produces=("text/plain; charset=utf-8"))
	public String checkPayUpdate(HttpServletRequest request, ModelMap model){
		System.out.println("！!_!_!_!_!!-----!!_____!_!!!_!!进入检查支付方法——!_!_!_!_!____!_!____！！——！——！——！__!_!_!!!");
		String token=request.getParameter("token");
		String sn=request.getParameter("orderNo");//订单编号
		String amount=request.getParameter("orderAmount");//订单金额
		String paymentOrderId=request.getParameter("paymentOrderId");//支付编号
		String orderDatetime=request.getParameter("orderDatetime");//订单提交时间
		String payDatetime=request.getParameter("payDatetime");//订单支付时间
		BigDecimal price=new BigDecimal(amount);
		price=price.divide(new BigDecimal("100"),2, BigDecimal.ROUND_HALF_UP);
		orderServiceImpl.updatePaymentStatus(sn, price,paymentOrderId,orderDatetime,payDatetime,getCurrent(),null,null);
//		return "订单编号： "+sn+"支付编号： "+paymentOrderId+"订单金额： "+amount+"提交时间： "+orderDatetime+"支付时间： "+payDatetime;
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("type", "success");
		String str= JsonUtils.toJson(result);
		System.out.println(str);
		return str;
	}
	
	
	@RequestMapping(value="/systemPayOrder")
	public void systemPayOrder(HttpServletRequest request,ModelMap model){
		System.out.println("————！!_!_!_!_!!!!_____!_!!!_!!进入支付后台回调方法——!_!_!_!_!____!_!____！！——！——！——！__!_!_!!!");
		String token=request.getParameter("token");
		String sn=request.getParameter("orderNo");//订单编号
		String amount=request.getParameter("orderAmount");//订单金额
		String paymentOrderId=request.getParameter("paymentOrderId");//支付编号
		String orderDatetime=request.getParameter("orderDatetime");//订单提交时间
		String payDatetime=request.getParameter("payDatetime");//订单支付时间
		BigDecimal price=new BigDecimal(amount);
		price=price.divide(new BigDecimal("100"),2, BigDecimal.ROUND_HALF_UP);
		//orderServiceImpl.updatePaymentStatus(sn, price,paymentOrderId,orderDatetime,payDatetime,getCurrent(),null,null);
		
		System.out.println("订单编号： "+sn);
		System.out.println("支付编号： "+paymentOrderId);
		System.out.println("订单金额： "+amount);
		System.out.println("提交时间： "+orderDatetime);
		System.out.println("支付时间： "+payDatetime);
	}
	/**
	 * @param sn
	 * @param token
	 * @param request
	 * @param model
	 */
	@ResponseBody
	@RequestMapping(value="/systemSearch")
	public String systemSearch(String sn, String token, HttpServletRequest request, ModelMap model){
		Order order=orderServiceImpl.findBySn(sn);
		String resultMsg=PaymentUtil.searchOrder(order, sn, token);
		System.out.println(resultMsg);
		return "{\"resultType\":\"success\",\"msg\":"+resultMsg+"}";
//		String signSrc="";
//		String serverUrl=PropertyUtil.getValue("serverUrl");
//		String receiveUrl=PropertyUtil.getValue("receiveUrl");
//		receiveUrl+=token;
//		String key=PropertyUtil.getValue("key"); 
//		String merchantId=PropertyUtil.getValue("merchantId");
//		String version=PropertyUtil.getValue("version");
//		String signType=PropertyUtil.getValue("signType");
//		String orderNo=sn;
//		String orderDatetime=DateTime.timeToString(order.getCreateDate());
//		String queryDatetime=DateTime.timeToString(new Date());
//		
//		StringBuffer bufSignSrc=new StringBuffer();
//		com.allinpay.ets.client.StringUtil.appendSignPara(bufSignSrc, "merchantId",merchantId);
//		com.allinpay.ets.client.StringUtil.appendSignPara(bufSignSrc, "version", version);
//		com.allinpay.ets.client.StringUtil.appendSignPara(bufSignSrc, "signType", signType);
//		com.allinpay.ets.client.StringUtil.appendSignPara(bufSignSrc, "orderNo", orderNo);
//		com.allinpay.ets.client.StringUtil.appendSignPara(bufSignSrc, "orderDatetime",orderDatetime);
//		com.allinpay.ets.client.StringUtil.appendSignPara(bufSignSrc, "queryDatetime",queryDatetime);
//		com.allinpay.ets.client.StringUtil.appendLastSignPara(bufSignSrc, "key", key);
//		signSrc=bufSignSrc.toString();
//		String sign="";
//		sign= com.allinpay.ets.client.SecurityUtil.MD5Encode(bufSignSrc.toString());
//		
//		Map<String, String> result=new HashMap<String,String>();
//		result.put("serverUrl", serverUrl);
//		result.put("receiveUrl", receiveUrl);
//		result.put("key", key);
//		result.put("merchantId", merchantId);
//		result.put("version", version);
//		result.put("signType", signType);
//		result.put("orderNo", orderNo);
//		result.put("orderDatetime", orderDatetime);
//		result.put("queryDatetime", queryDatetime);
//		result.put("signSrc", signSrc);
//		result.put("sign", sign);
//		String resultStr=JsonUtils.toJson(result);
//		
//		return resultStr;
		
	}
}
